import { Component, OnInit } from '@angular/core';
import { PersonnelService } from '../../../services/personnel.service'
import { ExceptionHandler } from 'src/app/handler/exception.handler';
import { NzMessageService } from 'ng-zorro-antd/message';
import { ActivatedRoute, Router } from '@angular/router';
import { DepartmentService } from '../../../services/department.service'
import { PersonnelInfo } from 'src/app/models';
@Component({
  selector: 'zepride-employee',
  templateUrl: './employee.component.html',
  styleUrls: ['./employee.component.less']
})
export class EmployeeComponent implements OnInit {

  departlist = []
  loading: boolean = true
  fconfirm: boolean = true
  userlist: Array<any> = []
  listOfOption = [
    { label: '姓名', value: '姓名' },
    { label: '工号', value: '工号' },
    { label: '部门', value: '部门' },
    { label: '职务', value: '职务' },
    { label: '工作状态', value: '工作状态' }
  ];
  text = null
  parentId = ''
  obj = {}
  selecttext = '筛选'
  params = {
    pageIndex: 1,
    countOfPage: 10,
    total: 0
  }
  constructor(
    private person: PersonnelService,
    private message: NzMessageService,
    private activatedRoute: ActivatedRoute,
    private departmentService: DepartmentService,
    private router: Router
  ) { }

  ngOnInit(): void {
    this.parentId = this.activatedRoute.snapshot.queryParams.departmentNodeId;
    this.getuserlist();
    this.getdepartlist();
  }


  // 获取user所有列表
  async getuserlist() {
    this.loading = true;
    try {
      if (this.parentId) {
        var { total, list } = await this.person.queryUsers({ pageIndex: this.params.pageIndex, countOfPage: this.params.countOfPage, parentId: this.parentId });
      } else {
        var { total, list } = await this.person.queryUsers({ pageIndex: this.params.pageIndex, countOfPage: this.params.countOfPage });
      }
      this.params.total = total;
      this.userlist = list;
      this.loading = false;
      this.userlist.forEach(item => {
        if (!item.departmentNode) {
          let departmentNode = {}
          const nodeName = ''
          departmentNode = { ...departmentNode, nodeName }
          item.departmentNode = { ...item.departmentNode, departmentNode }
        }
      })
      console.log(this.userlist);

    } catch (error) {
      this.loading = false
      ExceptionHandler.current.handleError(error);
    }
  }

  // 删除用户
  async deluer(data) {
    this.loading = true;
    if (data.acc.superPermits && data.acc.superPermits.length) {
      this.message.info("超级账号不能删除")
      return this.loading = false
    }
    try {
      await this.person.deleteUser(data.id);
      this.loading = false;
      this.userlist = this.userlist.filter(item => item.id !== data.id);
      this.message.success("删除用户成功");
    } catch (error) {
      ExceptionHandler.current.handleError(error);
      this.message.error("删除用户失败");
    }
    this.getuserlist();
  }

  // 搜索用户
  async serach() {
    if (!this.text) {
      return this.getuserlist()
    }
    try {
      const { list, total } = await this.person.queryUsers({ name: this.text, pageIndex: this.params.pageIndex, countOfPage: this.params.countOfPage });
      if (list.length == 0) {
        this.userlist = null;
        this.params.total = 0;
        return this.message.info("没有查询到该人员");
      }
      this.userlist = list;
      this.userlist.forEach(item => {
        if (!item.departmentNode) {
          let departmentNode = {}
          const nodeName = ''
          departmentNode = { ...departmentNode, nodeName }
          item.departmentNode = { ...item.departmentNode, departmentNode }
        }
      })
      this.params.total = total;
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }

  // 分页页码改变
  changePageindex(pageIndex) {
    this.params.pageIndex = pageIndex;
    if (this.text) {
      this.serach();
    } else {
      this.getuserlist();
    }
  }

  // 分页条数改变
  changePagesize(pagecount) {
    this.params.countOfPage = pagecount;
    if (this.text) {
      this.serach();
    } else {
      this.getuserlist();
    }
  }

  changeserach(nzVisible) {
    console.log(nzVisible);
  }

  clear() {
    this.text = '';
    this.params.pageIndex = 1;
    this.params.countOfPage = 10;
    this.getuserlist();
  }

  // 获取部门list
  async getdepartlist() {
    try {
      const { list } = await this.departmentService.queryDepartmentsList();
      this.departlist = list;
      console.log(list);
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }

  editEmployee(employee: PersonnelInfo) {
    this.router.navigate(['/home/employeeList/addemployeeList'], {
      queryParams: { id: employee.id }
    })
  }
}

